package flc.ast.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.demon.fmodsound.FmodSound;
import com.stark.audio.edit.AeConst;
import com.stark.audio.edit.fmod.FmodAudioPlayer;
import com.stark.audio.edit.fmod.FmodVoiceType;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import VideoHandle.EpAudio;
import VideoHandle.EpEditor;
import VideoHandle.OnEditorListener;
import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.VoiceAdapter;
import flc.ast.bean.MyVoiceBean;
import flc.ast.databinding.ActivityAudioEditBinding;
import flc.ast.utils.Content;
import flc.ast.view.BaseAvTrackView;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.usersys.IUserSysEvent;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.media.audio.AudioFormat;
import stark.common.basic.media.audio.AudioPlayerImpl;
import stark.common.basic.media.audio.IAudioPlayer;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.utils.WorkPathUtil;
import stark.common.basic.view.container.StkLinearLayout;

public class AudioEditActivity extends BaseAc<ActivityAudioEditBinding> implements IAudioPlayer.IListener {

    public static String audioPath;
    protected IAudioPlayer mAudioPlayer;
    private boolean isInitTrackViewDuration = false;
    private float mSpeed = 1.0f;
    private float mPitch = 1.0f;
    private long mStartTime = 0;
    private long mEndTime = 0;
    private long mTotalTime = 0;
    private VoiceAdapter voiceAdapter;
    private int oldPosition = 0;
    private int selEffect = FmodVoiceType.NORMAL.getType();
    private FmodAudioPlayer mFmodAudioPlayer;
    //转换为wav文件的临时文件路径
    private String mConvertWavPath;
    private Dialog mySaveDialog;
    private SeekBar seekBar;
    private TextView textView;
    private StkLinearLayout llAudioSaveSuc;


    @Override
    protected int onCreate() {
        return R.layout.activity_audio_edit;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAudioEditBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mAudioPlayer = createAudioPlayer();
        mAudioPlayer.play(audioPath);
        mAudioPlayer.setListener(this);

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.llAudioSpeed.setOnClickListener(this);
        mDataBinding.ivSpeedCancel.setOnClickListener(this);
        mDataBinding.ivSpeedConfirm.setOnClickListener(this);
        mDataBinding.ivMinus1.setOnClickListener(this);
        mDataBinding.ivPlus1.setOnClickListener(this);
        mDataBinding.ivMinus2.setOnClickListener(this);
        mDataBinding.ivPlus2.setOnClickListener(this);

        mDataBinding.sbSpeed.setMax(AeConst.SPEED_MAX_PROGRESS);
        mDataBinding.sbSpeed.setProgress(AeConst.getProgressBySpeed(mSpeed));
        mDataBinding.sbSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (!fromUser) {
                    return;
                }
                mSpeed = AeConst.getSpeedByProgress(progress);
                updateSpeedTxtValue();
                if (mAudioPlayer != null) {
                    mAudioPlayer.setSpeed(mSpeed);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mDataBinding.sbTone.setMax(AeConst.PITCH_MAX_PROGRESS);
        mDataBinding.sbTone.setProgress(AeConst.getProgressByPitch(mPitch));
        mDataBinding.sbTone.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (!fromUser) {
                    return;
                }
                mPitch = AeConst.getPitchByProgress(progress);
                updatePitchTxtValue();
                if (mAudioPlayer != null) {
                    mAudioPlayer.setPitch(mPitch);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mDataBinding.llAudioCut.setOnClickListener(this);
        mDataBinding.ivCutCancel.setOnClickListener(this);
        mDataBinding.ivCutConfirm.setOnClickListener(this);
        mDataBinding.ivSReduce.setOnClickListener(this);
        mDataBinding.ivSAdd.setOnClickListener(this);
        mDataBinding.ivEReduce.setOnClickListener(this);
        mDataBinding.ivEAdd.setOnClickListener(this);
        mDataBinding.trackView.setListener(new BaseAvTrackView.IListener() {
            @Override
            public void onStartTimeChanged(long startTime) {
                if (mAudioPlayer != null) {
                    mAudioPlayer.seekTo((int) startTime);
                    mAudioPlayer.resume();
                }
                mStartTime = startTime;
                mDataBinding.tvSTime.setText(TimeUtils.millis2String(startTime, "mm:ss"));
            }

            @Override
            public void onEndTimeChanged(long endTime) {
                if (mAudioPlayer != null) {
                    mAudioPlayer.seekTo((int) endTime);
                    mAudioPlayer.resume();
                }
                mEndTime = endTime;
                mDataBinding.tvETime.setText(TimeUtils.millis2String(endTime, "mm:ss"));
            }

            @Override
            public void onStartEndTimeChanged(long startTime, long endTime) {
                if (mAudioPlayer != null) {
                    mAudioPlayer.seekTo((int) startTime);
                    mAudioPlayer.resume();
                }
                mStartTime = startTime;
                mEndTime = endTime;
                mDataBinding.tvSTime.setText(TimeUtils.millis2String(startTime, "mm:ss"));
                mDataBinding.tvETime.setText(TimeUtils.millis2String(endTime, "mm:ss"));
            }
        });

        mDataBinding.llAudioMutate.setOnClickListener(this);
        mDataBinding.ivMutateCancel.setOnClickListener(this);
        mDataBinding.ivMutateConfirm.setOnClickListener(this);
        mDataBinding.ivMutatePlay.setOnClickListener(this);
        mDataBinding.rvAudioMutateList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        voiceAdapter = new VoiceAdapter();
        mDataBinding.rvAudioMutateList.setAdapter(voiceAdapter);
        voiceAdapter.setOnItemClickListener(this);

        mDataBinding.ivAudioEditSave.setOnClickListener(this);
        //保存弹框
        saveDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPlay://播放
                if (mAudioPlayer.isPlaying()) {
                    mAudioPlayer.pause();
                    mDataBinding.ivPlay.setImageResource(R.drawable.azantinzhong);
                } else {
                    mAudioPlayer.resume();
                    mDataBinding.ivPlay.setImageResource(R.drawable.abofangzhong2);
                }
                break;
            case R.id.llAudioSpeed://变速
                mDataBinding.llAudioEditBottom.setVisibility(View.GONE);
                mDataBinding.llAudioSpeedFunc.setVisibility(View.VISIBLE);
                break;
            case R.id.ivSpeedConfirm:
                setSpeed();
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioSpeedFunc.setVisibility(View.GONE);
                break;
            case R.id.ivSpeedCancel:
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioSpeedFunc.setVisibility(View.GONE);
                break;
            case R.id.ivMinus1://音速-
                clickSpeedMinusPlus(true);
                break;
            case R.id.ivPlus1://音速+
                clickSpeedMinusPlus(false);
                break;
            case R.id.ivMinus2://音调-
                clickPitchMinusPlus(true);
                break;
            case R.id.ivPlus2://音调+
                clickPitchMinusPlus(false);
                break;
            case R.id.llAudioCut://裁剪
                mDataBinding.llAudioEditBottom.setVisibility(View.GONE);
                mDataBinding.llAudioCutFunc.setVisibility(View.VISIBLE);
                break;
            case R.id.ivCutConfirm:
                setCut();
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioCutFunc.setVisibility(View.INVISIBLE);
                break;
            case R.id.ivCutCancel:
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioCutFunc.setVisibility(View.INVISIBLE);
                break;
            case R.id.ivSReduce://开始时间-
                if (mStartTime >= 1000) {
                    long reduce = mStartTime - 1000;
                    if (reduce >= 0) {
                        mStartTime = reduce;
                        mDataBinding.tvSTime.setText(TimeUtils.millis2String(mStartTime, "mm:ss"));
                        mDataBinding.trackView.setmStartTime(reduce);

                    } else {
                        ToastUtils.showShort(R.string.min_hint);
                    }
                } else {
                    ToastUtils.showShort(R.string.min_hint);
                }
                break;
            case R.id.ivSAdd://开始时间+
                if (mStartTime < mEndTime) {
                    long add = mStartTime + 1000;
                    if (add < (mEndTime - 1000)) {
                        mStartTime = add;
                        mDataBinding.tvSTime.setText(TimeUtils.millis2String(mStartTime, "mm:ss"));
                        mDataBinding.trackView.setmStartTime(add);
                    } else {
                        ToastUtils.showShort(R.string.start_time_hint);
                    }
                } else {
                    ToastUtils.showShort(R.string.max_hint);
                }
                break;
            case R.id.ivEReduce://结束时间-
                if (mEndTime > mStartTime && mEndTime <= mTotalTime) {
                    long reduce = mEndTime - 1000;
                    if (reduce - mStartTime > 1000) {
                        mEndTime = reduce;
                        mDataBinding.tvETime.setText(TimeUtils.millis2String(mEndTime, "mm:ss"));
                        mDataBinding.trackView.setmEndTime(reduce);
                    } else {
                        ToastUtils.showShort(R.string.end_max_hint);
                    }
                } else {
                    ToastUtils.showShort(R.string.min_hint);
                }
                break;
            case R.id.ivEAdd://结束时间+
                if (mEndTime < mTotalTime) {
                    long add = mDataBinding.trackView.getEndTime() + 1000;
                    if (add < mDataBinding.trackView.getDuration()) {
                        mEndTime = add;
                        mDataBinding.tvETime.setText(TimeUtils.millis2String(mEndTime, "mm:ss"));
                        mDataBinding.trackView.setmEndTime(add);
                    } else {
                        ToastUtils.showShort(R.string.max_hint);
                    }
                } else {
                    ToastUtils.showShort(R.string.max_hint);
                }
                break;
            case R.id.llAudioMutate://变声
                mDataBinding.llAudioEditBottom.setVisibility(View.GONE);
                mDataBinding.llAudioMutateFunc.setVisibility(View.VISIBLE);
                break;
            case R.id.ivMutateConfirm:
                setMutate();
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioMutateFunc.setVisibility(View.GONE);
                mFmodAudioPlayer.pause();
                break;
            case R.id.ivMutateCancel:
                mDataBinding.llAudioEditBottom.setVisibility(View.VISIBLE);
                mDataBinding.llAudioMutateFunc.setVisibility(View.GONE);
                mFmodAudioPlayer.pause();
                break;
            case R.id.ivMutatePlay:
                if (mFmodAudioPlayer.isPlaying()) {
                    mFmodAudioPlayer.pause();
                } else {
                    mFmodAudioPlayer.resume();
                }
                break;
            case R.id.ivAudioEditSave://保存
                saveAudio();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (voiceAdapter.getItem(position).isVip()) {
            UserSysEventProxy.getInstance().payEvent(this, new IUserSysEvent.IPayEventCallback() {
                @Override
                public void onPayOk(boolean isRealOk) {
                    if (isRealOk) {
                        //接入了用户系统，并且用户付费了或是vip了
                        //执行相应操作
                        voiceAdapter.getItem(oldPosition).setSelected(false);
                        voiceAdapter.notifyItemChanged(oldPosition);
                        oldPosition = position;
                        voiceAdapter.getItem(position).setSelected(true);
                        voiceAdapter.notifyItemChanged(position);
                        mFmodAudioPlayer.play(mConvertWavPath);
                        mFmodAudioPlayer.setAudioType(voiceAdapter.getItem(position).getType());
                        selEffect = voiceAdapter.getItem(position).getType();
                    } else {
                        ToastUtils.showShort(getString(R.string.not_vip));
                    }
                }
            });
        } else {
            voiceAdapter.getItem(oldPosition).setSelected(false);
            voiceAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            voiceAdapter.getItem(position).setSelected(true);
            voiceAdapter.notifyItemChanged(position);
            mFmodAudioPlayer.play(mConvertWavPath);
            mFmodAudioPlayer.setAudioType(voiceAdapter.getItem(position).getType());
            selEffect = voiceAdapter.getItem(position).getType();
        }
    }

    @Override
    protected void initData() {
        //转换格式播放
        String suffix = FileUtils.getFileExtension(audioPath);
        mFmodAudioPlayer = new FmodAudioPlayer(this);
        mFmodAudioPlayer.setListener(new IAudioPlayer.IListener() {
            @Override
            public void onUpdatePlayTime(int curPlayTime, int duration) {
                mDataBinding.sbMutatePlay.setMax((int) duration / 1000);
                mDataBinding.sbMutatePlay.setProgress(curPlayTime / 1000);
            }

            @Override
            public void onPlayChange(boolean isPlaying) {
                if (isPlaying) {
                    mDataBinding.ivMutatePlay.setImageResource(R.drawable.abofangzhong2);
                } else {
                    mDataBinding.ivMutatePlay.setImageResource(R.drawable.azantinzhong);
                }
            }
        });
        mConvertWavPath = audioPath;
        if (!suffix.equalsIgnoreCase("wav")) {
            convert2Wav();
        }
        //变声
        List<MyVoiceBean> listVoice = new ArrayList<>();
        listVoice.add(new MyVoiceBean(R.drawable.ali, FmodVoiceType.LOLITA.getType(), false));
        listVoice.add(new MyVoiceBean(R.drawable.acx, FmodVoiceType.UNCLE.getType(), false));
        listVoice.add(new MyVoiceBean(R.drawable.ahh, FmodVoiceType.CHORUS.getType(), true));
        listVoice.add(new MyVoiceBean(R.drawable.agx, FmodVoiceType.FUNNY.getType(), true));
        listVoice.add(new MyVoiceBean(R.drawable.akl, FmodVoiceType.ETHEREAL.getType(), true));
        voiceAdapter.setList(listVoice);
    }

    //保存图片
    private void saveAudio() {
        seekBar.setVisibility(View.VISIBLE);
        llAudioSaveSuc.setVisibility(View.GONE);
        textView.setText(getString(R.string.audio_save_ing));
        mySaveDialog.show();
        seekBar.setProgress(new Random().nextInt(60));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        FileP2pUtil.copyPrivateAudioToPublic(mContext, audioPath);
                        String path = PathUtils.getFilesPathExternalFirst() + Content.myAudio + "/" + FileUtils.getFileName(audioPath);
                        FileUtils.copy(audioPath, path);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        textView.setText(getString(R.string.pic_save_suc));
                        seekBar.setVisibility(View.GONE);
                        llAudioSaveSuc.setVisibility(View.VISIBLE);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                startActivity(HomeActivity.class);
                            }
                        }, 1000);
                    }
                });
            }
        }, 1000);
    }

    private void setSpeed() {
        String path = FileUtil.generateFilePath(Content.Temp, ".mp3");
        showDialog(getString(R.string.set_draw_ing));
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                EpAudio epAudio = new EpAudio(audioPath);
                epAudio.setPitch(mPitch);
                epAudio.setTempo(mSpeed);
                EpEditor.audioExec(epAudio, path, new OnEditorListener() {
                    @Override
                    public void onSuccess() {
                        emitter.onNext(true);
                    }

                    @Override
                    public void onFailure() {
                        emitter.onNext(false);
                    }

                    @Override
                    public void onProgress(float progress) {

                    }
                });
            }

            @Override
            public void accept(Boolean result) {
                dismissDialog();
                if (result) {
                    ToastUtils.showShort(getString(R.string.set_suc));
                    audioPath = path;
                    convert2Wav();
                    mAudioPlayer.play(audioPath);
                    isInitTrackViewDuration = false;
                }
            }
        });

    }

    private void setCut() {
        String path = FileUtil.generateFilePath(Content.Temp, ".mp3");
        showDialog(getString(R.string.audio_cut_ing));
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                EpAudio epAudio = new EpAudio(audioPath);
                float startTime = mDataBinding.trackView.getStartTime() / 1000f;
                float duration = mDataBinding.trackView.getEndTime() / 1000f - startTime;
                if (duration < 0) {
                    duration = 0;
                }
                epAudio.clip(startTime, duration);
                EpEditor.audioExec(epAudio, path, new OnEditorListener() {
                    @Override
                    public void onSuccess() {
                        emitter.onNext(true);
                    }

                    @Override
                    public void onFailure() {
                        emitter.onNext(false);
                    }

                    @Override
                    public void onProgress(float progress) {

                    }
                });
            }

            @Override
            public void accept(Boolean result) {
                dismissDialog();
                if (result) {
                    ToastUtils.showShort(getString(R.string.audio_cut_suc));
                    audioPath = path;
                    convert2Wav();
                    mAudioPlayer.play(audioPath);
                    isInitTrackViewDuration = false;
                }
            }
        });
    }

    //变声
    private void setMutate() {
        showDialog(getString(R.string.mutate_ing));
        String fileName = System.currentTimeMillis() + "_" + getString(R.string.ae_concat) + ".wav";
        String audioOutPath = WorkPathUtil.generateAudioFilePathByName(fileName);
        RxUtil.create(new RxUtil.Callback<Integer>() {
            @Override
            public void doBackground(ObservableEmitter<Integer> emitter) {
                int retCode = FmodSound.INSTANCE.saveSound(mConvertWavPath, selEffect, audioOutPath);
                emitter.onNext(retCode);
            }

            @Override
            public void accept(Integer code) {
                dismissDialog();
                if (code == 0) {
                    audioPath = audioOutPath;
                    mConvertWavPath = audioOutPath;
                    mAudioPlayer.play(audioPath);
                    ToastUtils.showShort(R.string.mutate_suc);
                } else {
                    ToastUtils.showShort(R.string.mutate_def);
                    FileUtils.delete(audioOutPath);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAudioPlayer != null) {
            mAudioPlayer.resume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mAudioPlayer != null) {
            mAudioPlayer.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mAudioPlayer != null) {
            mAudioPlayer.stop();
        }
        FileUtils.delete(PathUtils.getFilesPathExternalFirst() + Content.Temp);
        FileUtils.delete(mConvertWavPath);
        if (mFmodAudioPlayer != null) {
            mFmodAudioPlayer.stop();
            mFmodAudioPlayer.release();
        }
    }

    //将文件转换成wav
    private void convert2Wav() {
        String newFileName = FileUtils.getFileNameNoExtension(audioPath) + AudioFormat.WAV.getSuffix();
        String audioOutPath = WorkPathUtil.generateAudioFilePathByName(newFileName);
        EpEditor.audioFormatConvert(audioPath, audioOutPath, new OnEditorListener() {
            @Override
            public void onSuccess() {
                dismissDialog();
                mDataBinding.getRoot().post(new Runnable() {
                    @Override
                    public void run() {
                        mConvertWavPath = audioOutPath;
                    }
                });
            }

            @Override
            public void onFailure() {
                FileUtils.delete(audioOutPath);
                mConvertWavPath = null;
                ToastUtils.showShort(com.stark.common.res.R.string.convert_fail);
            }

            @Override
            public void onProgress(float progress) {
            }
        });
    }

    private void updatePitchTxtValue() {
        mDataBinding.tvToneValue.setText("+" + String.format("%.1f", mPitch));
    }

    private void updateSpeedTxtValue() {
        mDataBinding.tvSpeedValue.setText("x" + String.format("%.1f", mSpeed));
    }

    /**
     * 子类可覆盖重写，创建对应的播放器
     *
     * @return
     */
    @NonNull
    protected IAudioPlayer createAudioPlayer() {
        return new AudioPlayerImpl();
    }

    @Override
    public void onUpdatePlayTime(int curPlayTime, int duration) {
        if (!isInitTrackViewDuration) {
            isInitTrackViewDuration = true;
            mEndTime = duration;
            mDataBinding.trackView.setDuration(duration);
            mDataBinding.tvETime.setText(TimeUtils.millis2String(duration, "mm:ss"));
            mTotalTime = duration;
        }
        mDataBinding.trackView.setPlayTime(curPlayTime);
        mDataBinding.sbPlay.setMax((int) duration / 1000);
        mDataBinding.sbPlay.setProgress(curPlayTime / 1000);
        mDataBinding.tvPlayTime.setText(TimeUtil.getMmss(curPlayTime));
        mDataBinding.tvPlayLeftText.setText(TimeUtil.getMmss(curPlayTime));
        mDataBinding.tvPlayRightText.setText("/" + TimeUtil.getMmss(duration));
    }

    private void clickPitchMinusPlus(boolean isMinus) {
        float stepValue = AeConst.getPitchStepValue();
        mPitch += stepValue * (isMinus ? -1 : 1);
        if (mPitch < AeConst.PITCH_MIN) {
            mPitch = AeConst.PITCH_MIN;
        }
        if (mPitch > AeConst.PITCH_MAX) {
            mPitch = AeConst.PITCH_MAX;
        }
        updatePitchTxtValue();
        mDataBinding.sbTone.setProgress(AeConst.getProgressByPitch(mPitch));
        if (mAudioPlayer != null) {
            mAudioPlayer.setPitch(mPitch);
        }
    }

    private void clickSpeedMinusPlus(boolean isMinus) {
        float stepValue = AeConst.getSpeedStepValue();
        mSpeed += stepValue * (isMinus ? -1 : 1);
        if (mSpeed < AeConst.SPEED_MIN) {
            mSpeed = AeConst.SPEED_MIN;
        }
        if (mSpeed > AeConst.SPEED_MAX) {
            mSpeed = AeConst.SPEED_MAX;
        }
        updateSpeedTxtValue();
        mDataBinding.sbSpeed.setProgress(AeConst.getProgressBySpeed(mSpeed));
        if (mAudioPlayer != null) {
            mAudioPlayer.setSpeed(mSpeed);
        }
    }

    @Override
    public void onPlayChange(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.ivPlay.setImageResource(R.drawable.abofangzhong2);
        } else {
            mDataBinding.ivPlay.setImageResource(R.drawable.azantinzhong);
        }
    }

    //保存弹框
    private void saveDialog() {
        mySaveDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_audio_save, null);
        mySaveDialog.setContentView(inflate);
        mySaveDialog.setCancelable(true);
        Window dialogWindow = mySaveDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        seekBar = inflate.findViewById(R.id.sbProgress);
        textView = inflate.findViewById(R.id.tvAudioSaveTitle);
        llAudioSaveSuc = inflate.findViewById(R.id.llAudioSaveSuc);
    }

}